package gov.pbc.nn.kjgl.fxq.siteDocument.view;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.exception.NoneLoginException;
import com.bstek.bdf2.core.service.IDeptService;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

import gov.pbc.nn.core.bdf.entity.Dept;
import gov.pbc.nn.core.dao.CommonHibernateDao;
import gov.pbc.nn.kjgl.fxq.attachment.AttachmentEntity;
import gov.pbc.nn.kjgl.fxq.siteDocument.entity.SiteDocumentEntity;
import gov.pbc.nn.kjgl.fxq.workDocument.view.WorkDocumentPR;

@Component(SiteDocumentPR.BEAN_ID)
public class SiteDocumentPR extends CommonHibernateDao {
	public static final String BEAN_ID = "siteDocumentPR";
	
	@Autowired
	@Qualifier(IDeptService.BEAN_ID)
	private IDeptService deptService;
	
	@Resource(name = WorkDocumentPR.BEAN_ID)
	private WorkDocumentPR workDocumentPR;

	@DataProvider
	public void loadSiteDocuments(Page<SiteDocumentEntity> page, Criteria criteria) throws Exception {
		IUser user = ContextHolder.getLoginUser();
		if (user == null)
			throw new NoneLoginException("请先登录！");
		if (user.getDepts() == null || user.getDepts().isEmpty())
			throw new NoneLoginException("您未设置部门！");
		Dept dept = (Dept) user.getDepts().get(0);
		List<String> childrenIds = this.workDocumentPR.getAllChildOrgDeptId(dept.getCDept().getParentId());
		// System.out.println(dept.getCDept().getName());
		childrenIds.add(dept.getCDept().getId());
		DetachedCriteria detachedCriteria = this.buildDetachedCriteria(criteria, SiteDocumentEntity.class);
		detachedCriteria.add(Restrictions.or(Restrictions.eq("createUser", user.getUsername()), Restrictions.and(Restrictions.in("deptId", childrenIds), Restrictions.eq("state", 20))));
		detachedCriteria.add(Restrictions.eq("invalid", false));
		if (criteria != null && criteria.getOrders() != null && !criteria.getOrders().isEmpty()) {
			com.bstek.dorado.data.provider.Order order = criteria.getOrders().get(0);
			if (order.isDesc()) {
				detachedCriteria.addOrder(Order.desc(order.getProperty()));
			} else {
				detachedCriteria.addOrder(Order.asc(order.getProperty()));
			}
		} else {
			detachedCriteria.addOrder(Order.desc("createDate"));
		}
		this.pagingQuery(page, detachedCriteria);
	}

	@DataResolver
	@Transactional
	public void saveSiteDocuments(List<SiteDocumentEntity> siteDocuments) throws Exception {
		IUser user = ContextHolder.getLoginUser();
		if (user == null)
			throw new NoneLoginException("请先登录！");
		if (user.getDepts() == null || user.getDepts().isEmpty())
			throw new NoneLoginException("您未设置部门！");
		Dept dept = (Dept) user.getDepts().get(0);
		for (SiteDocumentEntity siteDocument : siteDocuments) {
			EntityState state = EntityUtils.getState(siteDocument);
			SiteDocumentEntity siteDocumentEntity = EntityUtils.toEntity(siteDocument);
			siteDocument.setState(10);
			if (state.equals(EntityState.NEW)) {
				siteDocument.setCheckOrg(this.deptService.loadDeptById(dept.getCDept().getParentId()).getName());
				siteDocument.setDeptId(dept.getCDept().getId());
				siteDocument.setDeptName(dept.getCDept().getName());
				this.save(siteDocument);
			} else if (state.equals(EntityState.MODIFIED)) {
				this.update(siteDocument);
			} else if (state.equals(EntityState.DELETED)) {
				this.delete(siteDocument);
			}

			List<AttachmentEntity> attachments = EntityUtils.getValue(siteDocumentEntity, "attachments");
			if (attachments != null) {
				this.update(siteDocument);
				for (AttachmentEntity attachmentEntity : attachments) {
					this.workDocumentPR.saveEntity(attachmentEntity);
				}
			}
		}

	}

	@Expose
	@DataResolver
	@Transactional
	public void submitSiteDocument(SiteDocumentEntity siteDocument) throws Exception {
		if (siteDocument.getState() != 20) {
			siteDocument.setState(20);
			siteDocument.setSubmitDate(new Date());
			this.update(siteDocument);
		}
	}
}
